package org.study.clickhouse.plus.boot.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.study.clickhouse.plus.boot.mapper.UserMapper;
import org.study.clickhouse.plus.boot.model.User;

import java.util.List;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2021-05-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectList(null);
    }

    @Override
    public PageInfo<User> startPage1(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.selectList(null);
        PageInfo<User> pageInfo = new PageInfo<>();
        if (userList != null) {
            pageInfo = new PageInfo<>(userList);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<User> startPage2(int pageNum, int pageSize) {
        Page<User> startPage = PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.selectList(null);
        PageInfo<User> pageInfo = new PageInfo<>();
        if (userList != null) {
            pageInfo = new PageInfo<>(startPage);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<User> offsetPage1(int pageNum, int pageSize) {
        PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        List<User> userList = userMapper.selectList(null);
        PageInfo<User> pageInfo = new PageInfo<>();
        if (userList != null) {
            pageInfo = new PageInfo<>(userList);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<User> offsetPage2(int pageNum, int pageSize) {
        Page<User> userPage = PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        List<User> userList = userMapper.selectList(null);
        PageInfo<User> pageInfo = new PageInfo<>();
        if (userList != null) {
            pageInfo = new PageInfo<>(userPage);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<User> doSelectPageJdk7(int pageNum, int pageSize) {
        Page<User> page = PageHelper.startPage(pageNum, pageSize)
                .doSelectPage(new ISelect() {
                    @Override
                    public void doSelect() {
                        userMapper.selectList(null);
                    }
                });
        PageInfo<User> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    @Override
    public PageInfo<User> doSelectPageJdk8(int pageNum, int pageSize) {
        Page<User> page = PageHelper.startPage(pageNum, pageSize)
                .doSelectPage(() -> userMapper.selectList(null));
        PageInfo<User> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    @Override
    public PageInfo<User> doSelectPageInfo(int pageNum, int pageSize) {
        PageInfo<User> pageInfo = PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> userMapper.selectList(null));
        return pageInfo;
    }

    @Override
    public long countJdk7() {
        long count = PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                userMapper.selectList(null);
            }
        });
        return count;
    }

    @Override
    public long countJdk8() {
        long count = PageHelper.count(() -> userMapper.selectList(null));
        return count;
    }
}
